<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nginx的301与302如何配置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nginx的301与302如何配置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先看一个完整代码示例，关于nginx 301 302跳转的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;301跳转设置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">listen 80;</div><div class="line">server_name 123.com;</div><div class="line">rewrite ^/(.*) http://456.com/<span class="variable">$1</span> permanent;</div><div class="line">access_log off;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;302跳转设置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">listen 80;</div><div class="line">server_name 123.com;</div><div class="line">rewrite ^/(.*) http://456.com/<span class="variable">$1</span> redirect;</div><div class="line">access_log off;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在看下关于nginx 301 302跳转的详细说明文档</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">server_name test.com;</div><div class="line">rewrite ^/(.*) http://www.test1.com/<span class="variable">$1</span> permanent;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;last – 基本上都用这个Flag。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;break – 中止Rewirte，不在继续匹配</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redirect – 返回临时重定向的HTTP状态302</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;permanent – 返回永久重定向的HTTP状态301</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Nginx的重定向用到了Nginx的HttpRewriteModule，下面简单解释以下如何使用的方法：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rewrite命令</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx的rewrite相当于apache的rewriterule(大多数情况下可以把原有apache的rewrite规则加上引号就可以直接使用)，它可以用在server,location 和IF条件判断块中,命令格式如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rewrite 正则表达式 替换目标 flag标记</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;flag标记可以用以下几种格式：</p>
<ul>
<li>last – 基本上都用这个Flag。</li>
<li>break – 中止Rewirte，不在继续匹配</li>
<li>redirect – 返回临时重定向的HTTP状态302</li>
<li>permanent – 返回永久重定向的HTTP状态301</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;特别注意：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;last和break用来实现URL重写，浏览器地址栏的URL地址不变，但是在服务器端访问的路径发生了变化；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;redirect和permanent用来实现URL跳转，浏览器地址栏会显示跳转后的URL地址；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如下面这段设定nginx将某个目录下面的文件重定向到另一个目录,$2对应第二个括号(.*)中对应的字符串：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">location /download/ &#123;</div><div class="line">    rewrite ^(/download/.*)/m/(.*)\..*$ <span class="variable">$1</span>/nginx-rewrite/<span class="variable">$2</span>.gz <span class="built_in">break</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx重定向的IF条件判断</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在server和location两种情况下可以使用nginx的IF条件判断，条件可以为以下几种：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;正则表达式</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如：匹配判断</p>
<ul>
<li>~ 为区分大小写匹配; !~为区分大小写不匹配</li>
<li>~* 为不区分大小写匹配；!~为不区分大小写不匹配</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (<span class="variable">$http_user_agent</span> ~ MSIE) &#123;</div><div class="line">    rewrite ^(.*)$ /nginx-ie/<span class="variable">$1</span> <span class="built_in">break</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;文件和目录判断</p>
<ul>
<li>-f和!-f判断是否存在文件</li>
<li>-d和!-d判断是否存在目录</li>
<li>-e和!-e判断是否存在文件或目录</li>
<li>-x和!-x判断文件是否可执行</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如下面设定nginx在文件和目录不存在的时候重定向：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (!-e <span class="variable">$request_filename</span>) &#123;</div><div class="line">    proxy_pass http://127.0.0.1;</div><div class="line">&#125;</div><div class="line"><span class="built_in">return</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回http代码，例如设置nginx防盗链：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">location ~* \.(gif|jpg|png|swf|flv)$ &#123;</div><div class="line">    valid_referers none blocked www.test.com www.test1.com;</div><div class="line">    <span class="keyword">if</span> (<span class="variable">$invalid_referer</span>) &#123;</div><div class="line">        <span class="built_in">return</span> 404;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Nginx/16. nginx的301与302如何配置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
